<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../../css/header.css" />
		<style>
			#scanComponent{
				width: 100%;
				top: 44px;
				bottom: 0px;
				position: absolute;
				text-align: center;
				background-color: black;
			}
		</style>
	</head>

	<body>
		
		<header class="mui-bar mui-bar-nav title">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: #FFFFFF;"></a>
		    <h1 class="mui-title title-color">扫一扫</h1>
		</header>
		
		<div id="scanComponent"></div>
		
		<script src="../../js/mui.min.js"></script>
		<script type="text/javascript" src="../../js/app.js" ></script>
		<script type="text/javascript">
			mui.init()
			
			mui.plusReady(function(){
				setTimeout("scanStart()", 500);
			});
			
			var scan = null;
			function scanStart() {
				try{
					var styles = {
						frameColor: "#007AFF",
						scanbarColor: "#F7F7F7",
						background: ""
					}
				
					scan = new plus.barcode.Barcode("scanComponent", [plus.barcode.QR], styles);
					scan.onmarked = onmarked;
					scan.onerror = onerror;
					scan.start();
				}catch(e) {
					app.showToast("扫码出错啦", "error");
				}
				
			}
			
			// 条码识别成功事件
		    function onmarked(type, result) {
//		        switch(type){
//		            case plus.barcode.QR:
//		            break;
//		            
//		            case plus.barcode.EAN13:
//		            break;
//		            
//		            case plus.barcode.EAN8:
//		            break;
//		        }
				scan.close();
				plus.webview.currentWebview().close();//关闭当前页面plus.webview.currentWebview().close();
		        
		        var newResult = new Array();
		        newResult = result.split(":")
		        var username = newResult[1];
		        if(app.getUserInfo().username == username) {
		        	app.showToast("不能添加自己为好友", "error");
		        }else{
		       		//app.showToast("扫码成功，username:" + username, "success");
					mui.ajax(app.serverUrl + "/simple-chat/api/user/username",{
						data:{
							myUserId: app.getUserInfo().userId,
							username: username
						},
						dataType:'json',
						type:'post',
						timeout:10000, 
						headers:{'Authorization':'Bearer ' + app.getToken()},
						success:function(data){
							if(data.status == 0) {
								var newUserInfo = data.data;
								
								console.log("sssss:" + JSON.stringify(newUserInfo));
								
								var imgCon = newUserInfo.avatar;
								var markName = newUserInfo.markName;
								var username = newUserInfo.username;
								var nickname = newUserInfo.nickname;
								var gender = newUserInfo.gender;
								var friend = newUserInfo.friend;
								var friendID = newUserInfo.userId;
								
								
								
								mui.openWindow({
				                    url: "feixin_user_tail.html",
				                    id: "feixin_user_tail.html",
				                    extras:{
				                      friendId: friendID,
				                      imgCon: imgCon,
				                      markName: markName,
				                      username: username,
				                      gender: gender,
				                      friend: friend,
				                      nickname: nickname
				                    }
				    
				                });
								return true;
							}else if(data.status == 6001){
								app.showToast("该用户不存在", "error");
								return false;
							}
							
							return false;
							extras
						},
						error:function(xhr,type,errorThrown){
							//异常处理；
							app.showToast("网络异常，请检查您的网络", "error");
						}
					});
		        }
		    }
		    
		    function onerror(e){  //错误弹框
                app.showToast("网络异常，请检查您的网络", "error");
                return false;
            };
		</script>
	</body>

</html>